
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
import {fail, ok} from "@/utils/responseUtil";
const db = cloud.database()
const _ = db.command

export default async function (ctx: FunctionContext) {
  /**
   * ["2"]
   */
  let body = ctx.body
  if (!nw.util.isArray(body)){
    return fail("数据不合法，参数必须是数组形式")
  }

  //校验是否存在已经授权
  let hasRoleCount = await nw.db.count({
    dbName:"adminRole",
    whereJson:{
      roleId:_.in(body),
      deleted:0
    }
  })
  if (hasRoleCount>0){
    return fail("存在用户已经授权的情况，无法进行删除")
  }

  //3.批量修改
  let result = await nw.db.update({
    dbName:"role",
    whereJson:{_id:_.in(body)},
    dataJson:{deleted:1}
  })

  if (result===0){
    return fail("删除失败")
  }

  return ok("删除成功")



}
